package com.chiao.action.admin;

import java.awt.Image;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.UUID;

import javax.imageio.ImageIO;
import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpMethod;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.InitBinder;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import com.chiao.core.BaseController;
import com.chiao.core.CommonUtils;
import com.chiao.core.ExtPageStore;
import com.chiao.core.IntEditor;
import com.chiao.entity.admin.Employee;
import com.chiao.service.admin.EmployeeService;
import com.chiao.util.Constant;
import com.sun.image.codec.jpeg.JPEGCodec;
import com.sun.image.codec.jpeg.JPEGImageEncoder;

@Controller
@RequestMapping(value = "/admin/emp")
public class EmployeeAction extends BaseController {

	@Autowired
	EmployeeService empService;

	@InitBinder
	public void initBinder(WebDataBinder binder) {
		binder.registerCustomEditor(int.class, new IntEditor());
	}

	@RequestMapping(value = "/add",method=RequestMethod.POST)
	public @ResponseBody String addDept(Employee e) {
		if (empService.addEmp(e) > 0) {
			return "{success:true}";
		} else {
			return "{failure:true}";
		}
	}
	
	@RequestMapping(value = "/update",method=RequestMethod.POST)
	public @ResponseBody String updateDept(Employee e) {
		if (empService.updateEmp(e) > 0) {
			return "{success:true}";
		} else {
			return "{failure:true}";
		}
	}
	
	@RequestMapping(value="/delete")
	public @ResponseBody int deleteEmpByIds(@RequestParam String ids){
		return empService.deleteEmpByIds(ids);
	}

	@RequestMapping(value = "/list")
	public @ResponseBody
	ExtPageStore list(@RequestParam int start, @RequestParam int limit, @RequestParam(required = false) String param) {
		ExtPageStore eps = new ExtPageStore();
		eps.setTotal(empService.countEmployee(param));
		eps.setItems(empService.queryEmp(start, limit, param));
		return eps;
	}

	@RequestMapping(value = "/listByDeptid")
	public @ResponseBody ExtPageStore list(@RequestParam int start, @RequestParam int limit, @RequestParam int deptid) {
		ExtPageStore eps = new ExtPageStore();
		eps.setTotal(empService.countEmployeeByDept(deptid));
		eps.setItems(empService.queryEmpByDept(start, limit, deptid));
		return eps;
	}

	@RequestMapping(value = "/photoupload")
	public @ResponseBody
	String flashupload(@RequestParam("file") MultipartFile file, HttpServletRequest request) throws IOException {
		String uploadpath = request.getSession().getServletContext().getRealPath(Constant.UPLOADPATH);
		uploadpath = CommonUtils.formatFilePath(uploadpath);
		String floder = CommonUtils.generateFloder();
		File f = new File(uploadpath + "/" + floder);
		if (!f.exists()) {
			f.mkdirs();
		}
		String orgname = file.getOriginalFilename();// 原始文件名
		String endfix = CommonUtils.getFileSuffix(orgname);// 文件后缀
		String newname = UUID.randomUUID().toString();
		String path = floder + "/" + newname + "." + endfix;
		String smallImage = uploadpath + "/" + path;
		Image src = ImageIO.read(file.getInputStream());
		int old_w = src.getWidth(null); // 得到源图宽
		int old_h = src.getHeight(null);
		float tagsize = 200;
		int new_w = 0;
		int new_h = 0; // 得到源图长
		float tempdouble;
		if (old_w > old_h) {
			tempdouble = old_w / tagsize;
		} else {
			tempdouble = old_h / tagsize;
		}
		new_w = Math.round(old_w / tempdouble);
		new_h = Math.round(old_h / tempdouble);// 计算新图长宽
		BufferedImage tag = new BufferedImage(new_w, new_h,
				BufferedImage.TYPE_INT_RGB);
		tag.getGraphics().drawImage(src, 0, 0, new_w, new_h, null); // 绘制缩小后的图
		FileOutputStream newimage = new FileOutputStream(smallImage); // 输出到文件流
		JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(newimage);
		encoder.encode(tag); // 近JPEG编码
		newimage.close();
		return (Constant.UPLOADPATH + "/" + path);
	}

}
